package com.wise.service.cms.content.menu;

import cn.hutool.core.collection.CollectionUtil;
import com.wise.entities.content.Menu;
import com.wise.framework.basic.EnumUtil;
import com.wise.framework.daomodel.DaoOne4Update;
import com.wise.framework.daomodel.DaoSave;
import com.wise.model.content.enums.MenuExtContentType;
import com.wise.model.content.enums.MenuType;
import com.wise.summer.v1.cms.content.menu.UpdateMenuById;
import com.wise.summer.v1.local.content.menutype.MenuTypeValidation;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.summerframework.model.SummerService;
import org.summerframework.model.SummerServiceBean;

import java.util.List;

import static com.wise.model.content.ContentErrMsgs.Err_000500000020;

@Service
@SummerService
public class UpdateMenuService implements SummerServiceBean<UpdateMenuById> {

	@Override
	public void sum(UpdateMenuById model) {
		List<Menu.ExtContent> extContentList = getAndCheckExtContent(model);

		Menu menu = DaoOne4Update.s(Menu.class, model.getId(), Err_000500000020);
		if (!StringUtils.isEmpty(model.getParentId())) {
			menu.setParentId(model.getParentId());
		}
		if (!StringUtils.isEmpty(model.getName())) {
			menu.setName(model.getName());
		}
		if (!StringUtils.isEmpty(model.getIcon())) {
			menu.setIcon(model.getIcon());
		}
//		if (!StringUtils.isEmpty(model.getUrl())) {
			menu.setUrl(model.getUrl());
//		}
		if (model.getSeqencing() != null) {
			menu.setSeqencing(model.getSeqencing());
		}
		if (model.getState() != null) {
			menu.setState(model.getState());
		}
		if (model.getShowStatus() != null) {
			menu.setShowStatus(model.getShowStatus());
		}
		if (model.getDescription() != null) {
			menu.setDescription(model.getDescription());
		}
		if (model.getTitlePic() != null) {
			menu.setTitlePic(model.getTitlePic());
		}
		if (model.getTitlePicHeight() != null) {
			menu.setTitlePicHeight(model.getTitlePicHeight());
		}
		menu.setType(model.getType());
		menu.setExtContent(model.getExtContent());
		menu.setNewRecommend(UpdateMenuService.getNewRecommendByExtContents(extContentList));
		menu.setAlias(model.getAlias());

		DaoSave.s(menu);
		model.setSummerResult(menu);
	}

	@SummerService(false)
	private List<Menu.ExtContent> getAndCheckExtContent(UpdateMenuById model) {
		return getAndCheckExtContent(model.getType(), model.getExtContent());
	}

	public static List<Menu.ExtContent> getAndCheckExtContent(Integer type, String extContent) {
		MenuType menuType = EnumUtil.valueOf(MenuType.class, type);
		try {
			Class validationClass = menuType.getValidationClass();
			if (null == validationClass) {
				return null;
			}
			MenuTypeValidation validation = (MenuTypeValidation) validationClass.newInstance();
			validation.setExtContent(extContent);
			validation.sum();

			return validation.getSummerResult();
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取新品推荐标志
	 * @param extContents 扩展信息
	 * @return
	 */
	static boolean getNewRecommendByExtContents(List<Menu.ExtContent> extContents) {
		if (CollectionUtil.isEmpty(extContents)) {
			return false;
		}
		for (Menu.ExtContent extContent : extContents) {
			if (MenuExtContentType.NEW_PRODUCT.isValue(extContent.getType())) {
				return true;
			}
		}
		return false;
	}
}
